grammar Xn;
//玄鸟
@header{
package com.telrob.antlr.role;
}

prog: progamBody+ ;

progamBody: declareBlock
		| functionBlock
		| string
		;

declareBlock: declare |assignment ;
//函数声明定义
functionBlock : functionHander '{' functionBody '}' ;
functionHander: baseType ID '(' functionParam? ')';
functionParam: functionParamDeclare (',' functionParamDeclare)* ;
functionParamDeclare: baseType assignmentDeclare  ;

//执行体
functionBody: calculateBody + ;

calculateBody: declareBlock | conditionIfBlock | whileBlock |doWhileBlock |forBlock |functionCall;

//函数调用
functionCall: ID '(' functionCallParam ')' ';'? ;
functionCallParam: expression (',' expression)* ;


//循环语句
whileBlock: 'while' '(' conditionBlock ')' '{' functionBody '}' ;
doWhileBlock: 'do' '{' functionBody '}' 'while' '(' conditionBlock ')' ';' ;
forBlock: 'for' '(' forConditionBlock ')' '{' functionBody '}' ;
forConditionBlock: declareBlock ';' conditionBlock ';' declareBlock ;


//判断语句
conditionIfBlock: ifBlock elseIfBlock * elseBlock? ;

ifBlock: 'if' '(' conditionBlock ')' '{' functionBody '}' ;
elseIfBlock: 'else' 'if' '(' conditionBlock ')' '{' functionBody '}' ;
elseBlock: 'else' '{' functionBody '}' ;


//条件
conditionBlock:	conditionBlock '&&' conditionBlock
			| conditionBlock '||' conditionBlock
			| '('condition ')'
			| '(' conditionBlock ')'
			| condition
			;
condition: expression ('>'|'>='|'<'|'<='|'!='|'==') expression ;

//声明
declare: baseType assignmentDeclare  (',' assignmentDeclare )* ';'? ;
assignmentDeclare: (assignment|ID) ;
//赋值
assignment: ID '=' expression ';'?;
expression: expression '%' expression
	| expression ('*'|'/') expression
	| expression ('+'|'-') expression
	| '(' expression ')'
	| expressionType
	;
expressionType: ID | Digit |string;

	
baseType: ('char'| 'int'| 'short'| 'long'|'double'|'float'|'string'|'void') ;
ID: [a-z][a-zA-Z_0-9]* ;
Digit: [0-9]+ ;
string: '"' ('""'|~'"'|'%'|'X'|'\\"' |'\\'|':')* '"' ; 


//多行注释
COMMENT: '/*' .*? '*/' ->skip ;

//单行注释
LINECOMMENT : '//' .*? '\n' ->skip;
//跳过空白和换行
WS: [ \t\r\n]+ ->skip ;

